<template>
  <a-modal
    :title="'加减币商户 ' + name"
    :visible="visible"
    @ok="onSubmit"
    @cancel="close"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-form-model-item ref="top_up" label="加减币" prop="top_up">
        <a-input-number
          v-model="form.top_up"
          :formatter="(value) => (value > 0 ? '+' + value : value)"
          :min="-origin_coins"
        />
        <span class="ml-2"> 余额: {{ origin_coins }}</span>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      id: null,
      name: '',
      // code: '',
      origin_coins: 0,
      form: {
        top_up: 0,
      },
      rules: {
        top_up: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    show(row) {
      this.origin_coins = row.coins
      this.id = row.id
      this.name = row.username
      // this.code = row.code
      this.form = {
        top_up: 0,
      }
      this.visible = true
    },
    close() {
      Object.assign(this.$data, this.$options.data())
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.axios
            .put(`/a/sellers/${this.id}/coins`, this.form)
            .then((res) => {
              if (res.data.code === 1) {
                this.close()
                this.$message.success('加减币成功')
                this.$emit('refresh')
              } else {
                this.$message.error(res.data.msg)
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
